﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace NKD07.Classes
{
    public class category:database
    {
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string imageUrl;

        public string ImageUrl
        {
            get { return imageUrl; }
            set { imageUrl = value; }
        }

        private string parent;

        public string Parent
        {
            get { return parent; }
            set { parent = value; }
        }

        private int hardSoft;

        public int HardSoft
        {
            get { return hardSoft; }
            set { hardSoft = value; }
        }

        private int longTerm;

        public int LongTerm
        {
            get { return longTerm; }
            set { longTerm = value; }
        }

        // get all categories from database
        public DataTable getAllCategories()
        {
            DataTable categories = new DataTable();
            OpenConnection();
            if (Connection != null)
            {
                string queryString = "select * from Category";
                try
                {
                    SqlDataAdapter adapter = new SqlDataAdapter(queryString, Connection);
                    adapter.Fill(categories);
                }
                catch (SqlException)
                {
                    categories = new DataTable();
                }
            }
            CloseConnection();
            return categories;
        }

        // get name based on id of category from database

        public string GetCategoryNameByID(int categoryID)
        {
            string result = null;
            OpenConnection();
            if (Connection != null)
            {
                string queryString = "select name from Category where id="+categoryID.ToString();
                try
                {
                    SqlDataAdapter adapter = new SqlDataAdapter(queryString, Connection);
                    DataTable tb = new DataTable();
                    adapter.Fill(tb);
                    result = tb.Rows[0][0].ToString();
                }
                catch (SqlException)
                {
                    result = null;
                }
            }
            CloseConnection();
            return result;
        }


    }
}